//
//  PhotoClass.m
//  UI10--HomeWork
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 YXY. All rights reserved.
//

#import "ShowBigPhoto.h"
#import "UIImageView+WebCache.h"
@implementation ShowBigPhoto


-(void)dealloc {
    [_array release];
    [_stepArray release];
    [super dealloc];
}


- (instancetype) initWithFrame:(CGRect)frame withArray:(NSMutableArray *)array withnum:(NSInteger)number withStepArray:(NSMutableArray *)stepArray{
    self = [super initWithFrame:frame];
    if (self) {
       self.array = array;
        self.stepArray = stepArray;
        [self createScollView: number];
    }
    return self;
}

- (void)createScollView:(NSInteger)number {
    [self.array insertObject:[self.array lastObject] atIndex:0];
    [self.array addObject:self.array[1]];
    self.num = self.array.count;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.pagingEnabled = YES;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.tag = 1001;
    if (self.array.count) {
        for (int i = 0; i < _array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, self.frame.size.height / 15, self.frame.size.width, self.frame.size.height)];
            imageView.tag = 2000 + i;
            [imageView sd_setImageWithURL:[NSURL URLWithString:_array[i]]];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClich:)];
            [imageView addGestureRecognizer:tap];
            
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longDidPress:)];
            longPress.allowableMovement = 100;
            [imageView addGestureRecognizer:longPress];
            
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.clipsToBounds = YES;
            [scrollView addSubview:imageView];
            [imageView release];
            
            UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width *i + self.frame.size.width / 2 - 20, self.frame.size.height / 20, self.frame.size.width / 5, 20)];
            NSString *str = [NSString stringWithFormat:@"%d",i];
            NSString *num = [NSString stringWithFormat:@"%ld", (unsigned long)_array.count - 2];
        
            numLabel.text = [[str stringByAppendingString:@"/"] stringByAppendingString:num];
            [scrollView addSubview:numLabel];
            [numLabel release];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width * i, self.frame.size.height / 1.7, self.frame.size.width, self.frame.size.height / 5)];
            label.textColor = [UIColor blackColor];
            label.numberOfLines = 0;
            label.text = _stepArray[i];
            [scrollView addSubview:label];
            [label release];
        }
    }
    scrollView.contentSize = CGSizeMake(self.frame.size.width * _array.count, 0);
    [scrollView setContentOffset:CGPointMake(self.frame.size.width * (number + 1), 0) animated:YES];
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self addSubview:scrollView];
    [scrollView release];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.x == self.frame.size.width * (self.num - 1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);

    }
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width * (self.num - 2), 0);
    }
}

- (void)tapClich:(UITapGestureRecognizer *)tap {
    [self.array removeObjectAtIndex:0];
    [self.array removeLastObject];
    [self.delegate dismiss];
    
}


//- (void)downloadDidPress:(UIButton *)sender {
//    UIScrollView *scrollView = (UIScrollView *)[self viewWithTag:1001];
//    UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:2000 + sender.tag - 3000];
//    UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//    sender.hidden = YES;
//}
//
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
//    if (error == nil) {
//        if (error == nil) {
//            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [alert show];
//            
//        }else{
//            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [alert show];
//        }
//    }
//}
#pragma mark - 下载图片
- (void)longDidPress:(UILongPressGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        self.downloadImage = (UIImageView *)recognizer.view;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要下载吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex) {
        UIImageWriteToSavedPhotosAlbum(self.downloadImage.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        alertView.hidden = YES;
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error == nil) {
        [ProgressHUD showSuccess:@"保存成功"];
    }else{
        [ProgressHUD showSuccess:@"保存失败"];
    }
}


@end
